package com.itfitness.stretchcalendarview.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class CalendarUtil {
    //获取每个月第一天为星期几
    public static int getMonthOneDayWeek(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DAY_OF_MONTH, 1);
//        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
//        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        return a.get(Calendar.DAY_OF_WEEK) - 1;
    }

    //获取当月有几天
    public static int getMonthMaxData(Calendar calendar) {
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 阳历天转农历天
     *
     * @param day
     * @return
     */
    public static String solarToLunar(int year, int month, int day) {
        Converter.Solar solar = new Converter.Solar();
        solar.solarYear = year;
        solar.solarMonth = month;
        solar.solarDay = day;
        Converter.Lunar lunar = Converter.solarToLunar(solar);
        return Converter.solarToLunarDay(lunar.lunarDay);
    }

    /**
     * 得到指定月的天数
     */
    public static int getMonthLastDay(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * @param year  年份
     * @param month 月份
     * @param day   日
     * @return 节日
     */
    public static String solarToFestival(int year, int month, int day) {
        //公历节日
        if (month == 1 && day == 1) {
            return "元旦节";
        }
        if (month == 2 && day == 14) {
            return "情人节";
        }
        if (month == 3 && day == 8) {
            return "妇女节";
        }
        if (month == 3 && day == 12) {
            return "植树节";
        }
        if (month == 3 && day == 15) {
            return "消费节";
        }
        if (month == 4 && day == 1) {
            return "愚人节";
        }
        if (month == 5 && day == 1) {
            return "劳动节";
        }
        if (month == 5 && day == 4) {
            return "青年节";
        }
        if (month == 5 && day == 10) {
            return "圣母节";
        }
        if (isMotherDay(year, month, day)) {
            return "母亲节";
        }
        if (month == 6 && day == 1) {
            return "儿童节";
        }
        if (month == 6 && day == 6) {
            return "爱眼日";
        }
        if (isFatherDay(year, month, day)) {
            return "父亲节";
        }
        if (month == 7 && day == 1) {
            return "建党日";
        }
        if (month == 8 && day == 1) {
            return "建军节";
        }
        if (month == 8 && day == 8) {
            return "爸爸节";
        }
        if (month == 9 && day == 10) {
            return "教师节";
        }
        if (month == 9 && day == 18) {
            return "九.一八";
        }
        if (month == 9 && day == 20) {
            return "和平日";
        }
        if (month == 10 && day == 1) {
            return "国庆节";
        }
        if (month == 10 && day == 6) {
            return "老人节";
        }
        if (month == 10 && day == 24) {
            return "联合国日";
        }
        if (month == 11 && day == 10) {
            return "世界青年";
        }
        if (month == 11 && day == 24) {
            return "感恩节";
        }
        if (month == 12 && day == 24) {
            return "平安夜";
        }
        if (month == 12 && day == 25) {
            return "圣诞节";
        }
        Converter.Solar solar = new Converter.Solar();
        solar.solarYear = year;
        solar.solarMonth = month;
        solar.solarDay = day;
        Converter.Lunar lunar = Converter.solarToLunar(solar);
        int lunarYear = lunar.lunarYear;
        int lunarMonth = lunar.lunarMonth;
        int lunarDay = lunar.lunarDay;
        if (lunarMonth == 1 && lunarDay == 1) {
            return "春节";
        }
        if (lunarMonth == 1 && lunarDay == 15) {
            return "元宵节";
        }
        if (lunarMonth == 2 && lunarDay == 2) {
            return "龙抬头";
        }
        if (lunarMonth == 4 && lunarDay == 4) {
            return "寒食节";
        }
        if (lunarMonth == 4 && lunarDay == 5) {
            return "清明节";
        }
        if (lunarMonth == 5 && lunarDay == 5) {
            return "端午节";
        }
        if (lunarMonth == 6 && lunarDay == 6) {
            return "天贶节";
        }
        if (lunarMonth == 8 && lunarDay == 15) {
            return "中秋节";
        }
        if (lunarMonth == 9 && lunarDay == 9) {
            return "重阳节";
        }
        if (lunarMonth == 12 && lunarDay == 8) {
            return "腊八节";
        }
        if (lunarMonth == 12 && lunarDay == 30) {
            return "除夕";
        }
        return "";
    }

    /**
     * @param year  年份
     * @param month 月份
     * @param day   日
     * @return 是否是母亲节(母亲节是五月份第二个星期日)
     */
    public static boolean isMotherDay(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, 4);
        int maxDate = c.getActualMaximum(Calendar.DATE);
        int sunDays = 0;
        for (int i = 1; i <= maxDate; i++) {
            c.set(Calendar.DATE, i);
            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                sunDays++;
                if (sunDays == 2) {
                    break;
                }
            }
        }
        return c.get(Calendar.YEAR) == year && c.get(Calendar.MONTH) + 1 == month && c.get(Calendar.DAY_OF_MONTH) == day;
    }

    /**
     * @param year  年份
     * @param month 月份
     * @param day   日
     * @return 是否是父亲节 (父亲节是六月份第三个星期日)
     */
    public static boolean isFatherDay(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, 5);
        int maxDate = c.getActualMaximum(Calendar.DATE);
        int sunDays = 0;
        for (int i = 1; i <= maxDate; i++) {
            c.set(Calendar.DATE, i);
            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                sunDays++;
                if (sunDays == 3) {
                    break;
                }
            }
        }
        return c.get(Calendar.YEAR) == year && c.get(Calendar.MONTH) + 1 == month && c.get(Calendar.DAY_OF_MONTH) == day;
    }

    //返回当前年份
    public static int getYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    //返回当月份
    public static int getMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1;
    }

    //返回当前几月几号
    public static int getCurrentDay() {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    //返回当前几月几号
    public static String getDay() {
        Calendar calendar = Calendar.getInstance();
        int m = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return String.valueOf(m) + "月" + day + "日(今天)";
    }

    //返回周几
    public static String getCurrentWeek() {
        String mWay;
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            //星期天老师直播
            return "周日";
        } else if ("2".equals(mWay)) {
            //星期一老师直播
            return "周一";
        } else if ("3".equals(mWay)) {
            //星期二老师直播
            return "周二";
        } else if ("4".equals(mWay)) {
            //星期三老师直播
            return "周三";
        } else if ("5".equals(mWay)) {
            //星期四老师直播
            return "周四";
        } else if ("6".equals(mWay)) {
            //星期五老师直播
            return "周五";
        } else if ("7".equals(mWay)) {
            //星期六老师直播
            return "周六";
        }
        return "周日";
    }

    //时间 calendar转化
    public static String formatDateYear(String time) {
        Calendar sourceCal = Calendar.getInstance();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sourceDate = null;
        try {
            sourceDate = formate.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sourceCal.setTime(sourceDate);
        return sourceCal.get(Calendar.YEAR) + "";
    }

    //时间 calendar转化
    public static String formatDateMonth(String time) {
        Calendar sourceCal = Calendar.getInstance();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sourceDate = null;
        try {
            sourceDate = formate.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sourceCal.setTime(sourceDate);
        return (sourceCal.get(Calendar.MONTH) + 1) + "";
    }

    //时间 calendar转化
    public static String formatDateDay(String time) {
        Calendar sourceCal = Calendar.getInstance();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sourceDate = null;
        try {
            sourceDate = formate.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sourceCal.setTime(sourceDate);
        return sourceCal.get(Calendar.DAY_OF_MONTH) + "";
    }

    //时间 calendar转化月日
    public static String formatDate(String time) {
        Calendar sourceCal = Calendar.getInstance();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        Date sourceDate = null;
        try {
            sourceDate = formate.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sourceCal.setTime(sourceDate);
        return
                (sourceCal.get(Calendar.MONTH) + 1) + "月" +
                        sourceCal.get(Calendar.DAY_OF_MONTH) + "日";
    }
    //时间 calendar转化年月日
    public static String formatDateYearMonthDay(String time) {
        Calendar sourceCal = Calendar.getInstance();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        Date sourceDate = null;
        try {
            sourceDate = formate.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sourceCal.setTime(sourceDate);
        return sourceCal.get(Calendar.YEAR)+"年"+
                (sourceCal.get(Calendar.MONTH) + 1) + "月" +
                        sourceCal.get(Calendar.DAY_OF_MONTH) + "日";
    }
}
